import curses
import random
import time

# 初始化屏幕
stdscr = curses.initscr()
curses.curs_set(0)
sh, sw = stdscr.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1)
w.timeout(100)

# 设置蛇的初始位置
snake = [(sh//2, sw//4), (sh//2, sw//4-1), (sh//2, sw//4-2)]
food = (sh//2, sw-10)
food_spawned = False

# 方向控制
direction = (0, 1)

def draw():
    w.clear()
    for segment in snake:
        w.addch(segment[0], segment[1], '#')
    w.addch(food[0], food[1], '*')
    w.refresh()

def input_handler():
    key = w.getch()
    if key == curses.KEY_UP and direction != (0, -1):
        direction = (0, -1)
    elif key == curses.KEY_DOWN and direction != (0, 1):
        direction = (0, 1)
    elif key == curses.KEY_LEFT and direction != (1, 0):
        direction = (1, 0)
    elif key == curses.KEY_RIGHT and direction != (-1, 0):
        direction = (-1, 0)

def logic():
    head_x, head_y = snake[0]
    new_head = (head_x + direction[0], head_y + direction[1])
    
    if new_head == food:
        snake.append((new_head[0], new_head[1]))
        food_spawned = False
    else:
        tail = snake.pop()
        snake.insert(0, new_head)
        
    if new_head[0] < 0 or new_head[0] >= sh or new_head[1] < 0 or new_head[1] >= sw or new_head in snake[1:]:
        return False  # Game Over
    
    if not food_spawned:
        food = (random.randint(1, sh-2), random.randint(1, sw-2))
        while food in snake:
            food = (random.randint(1, sh-2), random.randint(1, sw-2))
        food_spawned = True
    
    return True

while True:
    input_handler()
    if not logic():
        break
    draw()
    time.sleep(0.1)

curses.endwin() 
import random
import time

# 初始化屏幕
stdscr = curses.initscr()
curses.curs_set(0)
sh, sw = stdscr.getmaxyx()
w = curses.newwin(sh, sw, 0, 0)
w.keypad(1)
w.timeout(100)

# 设置蛇的初始位置
snake = [(sh//2, sw//4), (sh//2, sw//4-1), (sh//2, sw//4-2)]
food = (sh//2, sw-10)
food_spawned = False

# 方向控制
direction = (0, 1)

def draw():
    w.clear()
    for segment in snake:
        w.addch(segment[0], segment[1], '#')
    w.addch(food[0], food[1], '*')
    w.refresh()

def input_handler():
    key = w.getch()
    if key == curses.KEY_UP and direction != (0, -1):
        direction = (0, -1)
    elif key == curses.KEY_DOWN and direction != (0, 1):
        direction = (0, 1)
    elif key == curses.KEY_LEFT and direction != (1, 0):
        direction = (1, 0)
    elif key == curses.KEY_RIGHT and direction != (-1, 0):
        direction = (-1, 0)

def logic():
    head_x, head_y = snake[0]
    new_head = (head_x + direction[0], head_y + direction[1])
    
    if new_head == food:
        snake.append((new_head[0], new_head[1]))
        food_spawned = False
    else:
        tail = snake.pop()
        snake.insert(0, new_head)
        
    if new_head[0] < 0 or new_head[0] >= sh or new_head[1] < 0 or new_head[1] >= sw or new_head in snake[1:]:
        return False  # Game Over
    
    if not food_spawned:
        food = (random.randint(1, sh-2), random.randint(1, sw-2))
        while food in snake:
            food = (random.randint(1, sh-2), random.randint(1, sw-2))
        food_spawned = True
    
    return True

while True:
    input_handler()
    if not logic():
        break
    draw()
    time.sleep(0.1)

curses.endwin()